//package com.anlu.base.async;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.util.List;
//import java.util.concurrent.Callable;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.Future;
//
///**
// * <p>
// * Description: 对虚拟线程的封装
// * <p>
// * <p>
// *
// * @author anlu
// * @date 2024/9/23 10:45
// */
//public class VtTaskExecutor {
//
//
//    private static final Logger logger = LoggerFactory.getLogger(VtTaskExecutor.class);
//
//    private static class CallableWithRequestInfo implements Callable<Object>{
//        private CallableWithRequestInfo() {
//            //不允许创建空对象
//        }
//        private Callable<Object> callable;
//        public static CallableWithRequestInfo newInstance(Callable<Object> callable) {
//            CallableWithRequestInfo callableWithRequestInfo = new CallableWithRequestInfo();
//            callableWithRequestInfo.callable = callable;
//            return callableWithRequestInfo;
//        }
//
//        @Override
//        public Object call() throws Exception {
//            if(null == callable){
//                return null;
//            }
//
//            try{
//                return callable.call();
//            } catch (Exception e){
//                logger.error("call error",e);
//                throw e;
//            }
//
//        }
//    }
//
//    public static Object[] call(List<Callable<Object>>  callables){
//        Callable<Object>[] callableList = new Callable[callables.size()];
//        callables.toArray(callableList);
//        return call(callableList);
//    }
//
//    private static Object[] call(Callable<Object> ... calls) {
//        if(null == calls || calls.length == 0){
//            return new Object[0];
//        }
//
//        //默认用原始的
//        Callable<Object>[] callables = calls;
//
//        for (int i = 0; i < calls.length; i++) {
//            callables[i] = CallableWithRequestInfo.newInstance(calls[i]);
//        }
//
//        Object[] result = new Object[callables.length];
//
//        try(ExecutorService myExecutor = Executors.newVirtualThreadPerTaskExecutor()){
//            // 异步请求得到待聚合数据
//            Future<?>[] futureList = new Future<?>[callables.length];
//            for (int i = 0; i < callables.length; i++) {
//                Callable callable = callables[i];
//                Future<?> future =  myExecutor.submit(callable);
//                futureList[i] = future;
//            }
//
//            for (int i = 0; i < futureList.length; i++) {
//                try{
//                    result[i] = futureList[i].get();
//                } catch (Exception e){
//                    //如果调用失败，则设置结果为null
//                    result[i] = null;
//                    logger.info("VtTaskExecutor call has error",e);
//                }
//            }
//        }
//
//        return result;
//
//    }
//
//
//    public static void execute(Runnable runnable){
//        try (var executorService = Executors.newVirtualThreadPerTaskExecutor()){
//            executorService.execute(runnable);
//        }
//    }
//
//
//}
